# -*- coding:UTF-8 -*-
from flask import Flask, render_template
from werkzeug.exceptions import HTTPException
from src import init as init_framework
from etc.config import config
from src.common.loggings import configure_log
import src.backend as backend

__author__ = 'raptor'

__doc__ = """

"""

webSrv = Flask(__name__)


def init_app():
    webSrv.config.from_object(config['default'])

    config['default'].init_app(webSrv)

    configure_log(webSrv)
    if not webSrv.debug:
        webSrv.logger.warn("启动生产模式")

    init_framework(webSrv)
    # backend.init(webSrv)
    return webSrv


@webSrv.errorhandler(404)
def page_not_found(e):
    return render_template('error/404.html'), 404


@webSrv.errorhandler(Exception)
def handle_error(e):
    code = 500
    if isinstance(e, HTTPException):
        code = e.code
    if e:
        from traceback import format_exc
        webSrv.logger.error(format_exc())
    return render_template('error/500.html'), 500


@webSrv.errorhandler(503)
def service_unavailable(e):
    return render_template('error/503.html'), 503
